package MyIOtest;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;



public class test3 {
    //需求：把文件的数据排序
    //2-1-7-9-8
    //1-2-7-8-9
    public static void main(String[] args) throws IOException {
        //写入数据
        FileReader fr=new FileReader("D:\\javacode\\test\\c.txt");

        StringBuilder sb=new StringBuilder();
        int ch;
        while((ch=fr.read())!=-1){
            sb.append((char)ch);
        }
        fr.close();
        System.out.println(sb);
        //数据排序
        //转换成字符串进行切割存到字符串数组
        String str = sb.toString();
        String[] arrstr = str.split("-");

        //把字符串数组一个个强转成int类型数组
        ArrayList<Integer> list=new ArrayList<>();
        for (String s : arrstr) {
//            int i = Integer.parseInt(s);
            int i = Integer.parseInt(s);
            list.add(i);

        }

        //排序
        //sort默认升序
        Collections.sort(list);

        //写出数据
        FileWriter fw=new FileWriter("D:\\javacode\\test\\c.txt");

        for (int i = 0; i < list.size(); i++) {
            if(i==list.size()-1){
                fw.write(list.get(i)+"");
            }
            else{
                fw.write(list.get(i)+"-");
            }
        }
        fw.close();


    }
}
